package com.ithiema.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ServletKill")
public class ServletKill extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建一个同名的Cookie
        Cookie cookie = new Cookie("msg", "");
        //cookie的有效存活时间必须为0
        cookie.setMaxAge(0);
        //设置相同的有效访问路径
        cookie.setPath(request.getContextPath());
        //覆盖原来的cookie
        response.addCookie(cookie);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
